// pages/records/index.js
const app = getApp();
const api = require("../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    payList: [],
    consumeList: [],
    perPage: 15,
    payPage: 0,
    consumePage: 0,
    payEnd: false,
    consumeEnd: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    that.updateList()
    
    // var rechargeList = [], consumeList = [];
    // for(var i = 0; i < 10; ++i) {
    //   rechargeList.push({id: (i + 1), money: 100, dateTime: '2019-01-01 12:01:23'})
    //   consumeList.push({id: (i + 1), time: '20m', dateTime: '2019-01-01 12:01:23'})
    // }
    // this.setData({
    //   rechargeList: rechargeList,
    //   consumeList: consumeList
    // })
  },

  updateList: function() {
    var that = this,
      url = api.getPayRecordUrl,
      page = that.payPage,
      perPage = that.perPage
    if(that.data.active == 1) {
      url = api.getConsumeRecordUrl
      page = that.consumePage
      if (this.data.consumeEnd) {
        wx.showToast({
          icon: 'none',
          title: '没有更多了',
        })
        return false;
      }
    } else {
      if (this.data.payEnd) {
        wx.showToast({
          icon: 'none',
          title: '没有更多了',
        })
        return false;
      }
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        pageIndex: page,
        pageSize: perPage
      },
      success: function (res) {
        wx.hideLoading()
        // console.log(res, url)
        if (res.data.Code === 1) {
          if(that.data.active === 0) {
            var list = res.data.Entity
            that.setData({
              payList: res.data.Entity,
              payPage: that.data.payPage + 1
            })
          } else {
            that.setData({
              consumeList: res.data.Entity,
              consumePage: that.data.consumePage + 1
            })
          }
          if(res.data.Entity.length < that.data.perPage) {
            wx.showToast({
              icon: 'none',
              title: '没有更多记录了',
            })
          }
        }
      },
      complete: function (res) {
        
      }
    })
  },
  formatDateTime: function (dateTime) {
    var tmp = dateTime.split('T')
    return tmp[0] + ' ' + tmp[1]
  }, 

  onChange: function(event) {
    this.setData({
      active: event.detail.index
    })
    this.updateList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})